package com.souku.finance.application.ui.more;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.manager.RequestManager;
import com.souku.finance.application.R;
import com.souku.finance.application.base.BaseActivity;
import com.souku.finance.application.http.UrlConstact;
import com.souku.finance.application.utils.JsonUtil;
import com.souku.finance.application.utils.ParamUtil;
import com.souku.finance.application.utils.ToastUtil;
import com.souku.finance.application.utils.ViewUtil;
import com.souku.finance.application.http.volley.ReqListener;
import com.souku.finance.application.http.volley.Success;

import java.util.HashMap;

/**
 * Created by Android on 2016/12/22.
 */

public class ResetLoginPassActivity extends BaseActivity implements View.OnClickListener {

    protected RelativeLayout topBack;
    protected TextView topTitle;
    protected EditText etPass;
    protected Button btnOk;
    protected TextView tvShow;
    protected LinearLayout llShow;
    protected TextView tvForget;
    protected ImageView ivDelet;
    protected CheckBox checkbox;
    private int step = 0;
    private String oldPassword = "";

    @Override
    public void internet() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.act_resetloginpass);
        initView();
        setData();
        topTitle.setText("修改登录密码");
        ViewUtil.setDelete(etPass, ivDelet, null);
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.top_back) {
            onBackPressed();
        } else if (view.getId() == R.id.btn_ok) {
            String pass = etPass.getText().toString().trim();
            if (step == 0) {
                if (pass.isEmpty()) {
                    ToastUtil.show("请输入原密码");
                    return;
                }
                validate_password(pass);
            } else {
                if (pass.isEmpty()) {
                    ToastUtil.show("请输入新密码");
                    return;
                }
                if (pass.length()<8) {
                    ToastUtil.show("密码长度不小于8位");
                    return;
                }
                if (pass.equals(oldPassword)) {
                    ToastUtil.show("新旧密码不能相同");
                    return;
                }
                set_new_password(pass);
            }
        } else if (view.getId() == R.id.ll_show) {
            checkbox.setChecked(!checkbox.isChecked());
            if (!checkbox.isChecked()) {
                etPass.setInputType(129);
            } else {
                etPass.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
            }
            etPass.setSelection(etPass.getText().toString().trim().length());
        } else if (view.getId() == R.id.tv_forget) {
            Intent intent = new Intent(this, ForgetLoginPassActivity.class);
            startActivity(intent);
        }
    }

    private void initView() {
        topBack = (RelativeLayout) findViewById(R.id.top_back);
        topBack.setOnClickListener(ResetLoginPassActivity.this);
        topTitle = (TextView) findViewById(R.id.top_title);
        etPass = (EditText) findViewById(R.id.et_pass);
        btnOk = (Button) findViewById(R.id.btn_ok);
        btnOk.setOnClickListener(ResetLoginPassActivity.this);
        tvShow = (TextView) findViewById(R.id.tv_show);
        llShow = (LinearLayout) findViewById(R.id.ll_show);
        llShow.setOnClickListener(ResetLoginPassActivity.this);
        tvForget = (TextView) findViewById(R.id.tv_forget);
        tvForget.setOnClickListener(ResetLoginPassActivity.this);
        ivDelet = (ImageView) findViewById(R.id.iv_delet);
        checkbox = (CheckBox) findViewById(R.id.checkbox);
    }

    private void setData() {
        switch (step) {
            case 0:
                etPass.setText(oldPassword);
                etPass.setHint("当前登录密码");
                btnOk.setText("下一步");
                tvForget.setVisibility(View.VISIBLE);
                break;
            case 1:
                etPass.setText("");
                etPass.setHint("输入8-20位登录密码");
                btnOk.setText("完成");
                tvForget.setVisibility(View.GONE);
                break;
        }
    }

    @Override
    public void onBackPressed() {
        if (step != 0) {
            step--;
            setData();
        } else {
            super.onBackPressed();
        }
    }

    private void validate_password(final String pwd) {
        HashMap<String, String> map = ParamUtil.getParams();
        map.put("userPwd", pwd);
        RequestManager.getInstance().post(UrlConstact.VALIDATE_PASSWORD, ParamUtil.getSing(map), new ReqListener(this, new Success() {
            @Override
            public void onSuccess(String response) {
                if (JsonUtil.getCode(response) == 1) {
                    oldPassword=pwd;
                    step++;
                    setData();
                } else {
                    ToastUtil.show(JsonUtil.getMessage(response));
                }
            }
        }), ParamUtil.getHeaders());
    }

    private void set_new_password(final String pwd) {
        HashMap<String, String> map = ParamUtil.getParams();
        map.put("userPwd", pwd);
        RequestManager.getInstance().post(UrlConstact.SET_NEW_PASSWORD, ParamUtil.getSing(map), new ReqListener(this, new Success() {
            @Override
            public void onSuccess(String response) {
                ToastUtil.show(JsonUtil.getMessage(response));
                if (JsonUtil.getCode(response) == 1) {
                    finish();
                }
            }
        }), ParamUtil.getHeaders());
    }
}
